package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}14:56
 */
public class Interview53 {

    public static void main(String[] args) {
        int[] arr = {5,7,7,8,8,10};
        System.out.println(new Interview53().search(arr, 8));
    }

    public int search(int[] nums, int target) {
        return binarySearch(nums, target + 0.5) - binarySearch(nums, target - 0.5);
    }

    private int binarySearch(int[] nums, double target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {//二分查找
            int mid = (right + left) >>> 1;//无符号/2
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            }
        }
        return left;
    }
}
